//
//  WelcomeViewController.swift
//  weibo
//
//  Created by sanya on 15/6/26.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout
class WelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置头像
        if let urlString = sharedUserAccount?.avatar_large{
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 设置头像出现的动画
        bottomContraint?.constant = -(UIScreen.mainScreen().bounds.height-200)
        
        UIView.animateWithDuration(1.5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                UIView.animateWithDuration(0.8, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (__) -> Void in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SYSwitchRootVCNotification, object: true)
                })
        }
    }
    var bottomContraint:NSLayoutConstraint?
    override func loadView() {
        view = UIView()
        let backImageView = UIImageView(image: UIImage(named: "ad_background"))
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        // 设置自动布局
        backImageView.ff_Fill(view)
        // 记录 iconView 上添加的约束数组
        let cons = iconView.ff_AlignInner(ff_AlignType.BottomCenter, referView: view, size: CGSize(width: 90, height: 90), offset: CGPoint(x: 0, y: -160))
        // 在cons 数组中中查找iconView 的底部约束
        bottomContraint = iconView.ff_Constraint(cons, attribute: NSLayoutAttribute.Bottom)
        label.ff_AlignVertical(ff_AlignType.BottomCenter, referView: iconView, size: nil, offset: CGPoint(x: 0, y: 8))
    }
    
    lazy var iconView:UIImageView = {
        let iv = UIImageView(image: UIImage(named:"avatar_default_big"))
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 45
        return iv
    
    }()
    lazy var label:UILabel = {
        let label = UILabel()
        label.text = "欢迎归来"
        label.sizeToFit()
        label.alpha = 0
        
        return label
    }()

}
